#include "GameTestState.h"

GameTestState::GameTestState(BeanieCore* a_core, bool a_renew)
{
	m_core = a_core;

	m_name = "GameTestState";
	m_selfId = GAMETEST;
	m_swapId = GAMETEST;
	m_completed = false;
	m_renew = a_renew;
}

void GameTestState::init()
{
	m_world.init(CELL_COL,CELL_ROW);
	
	GameObject tObj;
	clkObjectDef tDef;
	for(int i = 0; i < OBJ_AMOUNT; i++)
	{
		tDef.m_type = SHAPE_RECT;
		tDef.m_width = 16.0f;
		tDef.m_height = 16.0f;

		tDef.m_pos.set(
			randomFloat(-OBJ_AMOUNT,OBJ_AMOUNT),
			randomFloat(-OBJ_AMOUNT,OBJ_AMOUNT));
		tDef.m_rot = 0.0f;

		tDef.m_mass = 1.0f;
		tDef.m_iner = 1.0f;

		tDef.m_lnVel.set(
			randomFloat(-OBJ_AMOUNT,OBJ_AMOUNT),
			randomFloat(-OBJ_AMOUNT,OBJ_AMOUNT));
		tDef.m_lnDamp = 5.0f;
		tDef.m_mxLnFrc = 512.0 * 3.0f;

		tDef.m_agVel = randomFloat(-360.0f,360.0f);
		tDef.m_agDamp = 10.0f;
		tDef.m_mxAgFrc = 512.0f * 2.0f;

		tDef.m_isAsleep = false;
		tDef.m_isCollidable = true;
		tDef.m_isStationary = false;

		tObj.initBody(m_world.createObject(tDef));
		tObj.initTextureX(m_core,SPR_DRONE,V2D_ONES,0.0f);

		m_objs.insertBack(tObj);
	}
}

void GameTestState::release()
{
	m_world.destroy();
}

void GameTestState::input()
{
	float time = m_core->getElapsed();

	static V2DF cam;

	if(m_core->keyInput(KEY_W))
		cam.y -= CAM_SPEED * time;
	if(m_core->keyInput(KEY_A))
		cam.x -= CAM_SPEED * time;
	if(m_core->keyInput(KEY_S))
		cam.y += CAM_SPEED * time;
	if(m_core->keyInput(KEY_D))
		cam.x += CAM_SPEED * time;

	m_core->updateCamera(cam);
}

void GameTestState::update()
{
	float time = m_core->getElapsed();

	m_world.updatePhysics(time);
	m_world.generateContacts(time);
	m_world.resolveContacts(time);

	for(int i = 0; i < m_objs.size(); i++)
		m_objs[i].update();
}

void GameTestState::draw()
{
	for(int i = 0; i < m_objs.size(); i++)
		m_objs[i].draw();
}

void GameTestState::preLoop()
{

}

void GameTestState::postLoop()
{
	
}